Coverage Report

Created: 2024-12-26 12:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\file\content.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use itertools::Itertools;
30
use std::fmt::Display;
31
32
pub struct Content<'a> {
33
    header: Option<&'a str>,
34
    body: String,
35
    footer: Option<&'a str>,
36
}
37
38
impl<D: Display> FromIterator<D> for Content<'_> {
39
111
    fn from_iter<T: IntoIterator<Item = D>>(iter: T) -> Self {
40
111
        Self {
41
111
            header: None,
42
111
            body: iter.into_iter().join("\n"),
43
111
            footer: None,
44
111
        }
45
111
    }
46
}
47
48
impl<'a> Content<'a> {
49
112
    pub fn try_from_iter<D: Display, E>(iter: impl Iterator<Item = Result<D, E>>) -> Result<Self, E> {
50
112
        let data = iter.collect::<Result<Vec<D>, E>>()
?0
;
51
112
        Ok(Self {
52
112
            header: None,
53
112
            body: data.iter().join("\n"),
54
112
            footer: None,
55
112
        })
56
112
    }
57
58
0
    pub fn from_string(value: String) -> Self {
59
0
        Self {
60
0
            header: None,
61
0
            body: value,
62
0
            footer: None,
63
0
        }
64
0
    }
65
66
0
    pub fn header(&mut self, header: &'a str) -> &mut Self {
67
0
        self.header = Some(header);
68
0
        self
69
0
    }
70
71
0
    pub fn footer(&mut self, footer: &'a str) -> &mut Self {
72
0
        self.footer = Some(footer);
73
0
        self
74
0
    }
75
76
223
    pub fn to_string(&self) -> Option<String> {
77
223
        if self.body.is_empty() {
  Branch (77:12): [True: 127, False: 96]
  Branch (77:12): [Folded - Ignored]
78
127
            return None;
79
96
        }
80
96
        Some(match (self.header, self.footer) {
81
0
            (Some(header), None) => format!("{header}{}", self.body),
82
0
            (Some(header), Some(footer)) => format!("{header}{}{footer}", self.body),
83
0
            (None, Some(footer)) => format!("{}{footer}", self.body),
84
96
            (None, None) => self.body.to_string(),
85
        })
86
223
    }
87
}